package cn.youyeku.zsxh.ui.activity.tabAct;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.adapter.RefreshFootAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.Help;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.AbsSwipeActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by youye on 17-4-4.
 */

public class ActMutualAccepted extends AbsSwipeActivity {


    FloatingActionsMenu btn_loading_fabMenu ;
    public ActMutualAccepted() {
        super(R.id.fgm_mutual_platform_recycle, R.id.fgm_mutual_platform_swipe);

    }

    @Override
    protected RefreshFootAdapter getAapter() {
        return new RefreshFootAdapter<Help>(R.layout.fgm_mutual_help_item,R.layout.refresh_footer) {

            @Override
            protected void onBindCustomerData(RefreshViewHolder holder, Help data, int pos) {
                ImageView img_face = holder.getChildView(R.id.mutual_imgV_head);
                TextView mutual_textV_nickname = holder.getChildView(R.id.mutual_textV_nickname);
                TextView mutual_textV_time = holder.getChildView(R.id.mutual_textV_time);
                TextView mutual_textV_repay = holder.getChildView(R.id.mutual_textV_repay);
                TextView mutual_textV_address = holder.getChildView(R.id.mutual_textV_address);
                TextView mutual_textV_event = holder.getChildView(R.id.mutual_textV_event);

                Glide.with(getApplicationContext())
                        .load(XhApi.getFace(data.getHelp_author_uid()))
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .into(img_face);
                mutual_textV_repay.setText("回报" + data.getHelp_reward());
                mutual_textV_address.setText("地址" + data.getHelp_address());
                mutual_textV_event.setText("事件" + data.getHelp_content());
                mutual_textV_nickname.setText(data.getNick_name());
                mutual_textV_time.setText("时间：" + data.getHelp_time());
            }

            @Override
            protected void setmOnItemClick(View itemView, final Help data, int pos) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIControl.showActMutualHelp(getApplicationContext(), data);
                    }
                });
            }
        };
    }

    @Override
    protected void requestData() {

        XhApi.get_help_accepted(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();

                JSONObject object = null;
                try {
                    object = new JSONObject(data);
                    int code = object.getInt("code");

                    LogUtils.w("code  == " + code + data);
                    if (code==200){
                        Gson gson = new Gson();
                        mData = gson.fromJson(object.getString("data"), new TypeToken<List<Help>>() {
                        }.getType());
                        Message msg = new Message();
                        msg.what = code;
                        handler.sendMessage(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
//       先set视图
        setContentView(R.layout.fgm_mutual_help);
        btn_loading_fabMenu = (FloatingActionsMenu) findViewById(R.id.btn_loading_fabMenu);
        btn_loading_fabMenu.setVisibility(View.GONE);
        super.onCreate(savedInstanceState);
    }


}
